package com.woke.method;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.text.TextUtils;
import android.util.Log;

import com.alibaba.fastjson.JSONArray;
import com.http.HttpMethods;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.woke.data.Datas_event2;
import com.woke.serizedatas.Datas_load;
import com.wokeMy.view.model.Constant;

import org.apache.http.Header;
import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;

public class Httploaddata {
    private static String servifee = "f";
    private static SharedPreferences sharep;
    public static String uploadFile(final MyApp myApp) {
        sharep = myApp.getSharedPreferences("loaduser", Context.MODE_PRIVATE);
//		application = (MyApp) getApplication();

        final String name = sharep.getString("name", "");
        final String password = sharep.getString("password", "");
        if (TextUtils.isEmpty(name) || TextUtils.isEmpty(password)) {
            return servifee;
        }

        AsyncHttpClient ahc = new AsyncHttpClient();
        RequestParams params = new RequestParams();
        params.put("g", "api");
        params.put("m", "user");
        params.put("a", "login");
        params.put("username", name);
        params.put("password", password);
        params.put("registerid", myApp.getregid());
        ahc.get(HttpMethods.BASEURL, params, new JsonHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers,
                                  JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                try {
                    String flag = response.getString("flag");
                    String info = response.getString("info");
                    if (flag.equals("success")) {
                        JSONObject jsonObject = response.getJSONObject("data");
                        String string = jsonObject.getString("data");
                        Datas_load datas_load = JSONArray.parseObject(string, Datas_load.class);
                        myApp.setDatas_load(datas_load);
                        Datas_event2 evens = new Datas_event2();
                        evens.str1 = "load_suess";
                        EventBus.getDefault().post(evens);
                        Editor edit = sharep.edit();
                        edit.putString("name", name);
                        edit.putString("password", password);
                        edit.commit();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers,
                                  String responseString, Throwable throwable) {
                super.onFailure(statusCode, headers, responseString, throwable);
            }

            @Override
            public void onStart() {
                // TODO Auto-generated method stub
                super.onStart();
            }

            @Override
            public void onFinish() {
                super.onFinish();
            }
        });
        return servifee;

    }
    // 系统开关 隐藏 P2P信贷、借款、还款、理财等
    public static void sysKguan(SharedPreferences share,String  appName) {
        sharep = share;
        int user_from;
        switch (appName) {
            case "中财在线":
                user_from = 1;
                break;
            case "赢聚付":
                user_from = 2;
                break;
            case "优先在线":
                user_from = 3;
                break;
            case "铭祺源":
                user_from = 6 ;
                break;
            case "掌鑫宝":
                user_from = 7;
                break;
            default:
                user_from = 1;
                break;
        }
        AsyncHttpClient ahc = new AsyncHttpClient();
        RequestParams params = new RequestParams();
        params.put("type",1); // 1.安卓 2.iOS
        params.put("oem_id",user_from);
        Log.e("MyApp系统开关（显示隐藏某些功能)rams",params.toString());
        //url:   parmas：请求时携带的参数信息   responseHandler：是一个匿名内部类接受成功过失败
        ahc.post(Constant.SYSTEKG, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                //判断状态码
                if(i == 200){
                    //获取结果
                    try {
                        String result = new String(bytes, "utf-8");
                        //                        Toast.makeText(CreditRepayActivity.this, result, Toast.LENGTH_SHORT).show();
                        Log.e("App系统开关（显示隐藏某些功能）Result", result);
                        try {
                            JSONObject jsonObject = new JSONObject(result);
                            if (jsonObject.get("flag").equals("success")) {
                                String status=jsonObject.getJSONObject("data").getJSONObject("data").getString("status");
                                String version=jsonObject.getJSONObject("data").getJSONObject("data").getString("version");
                                Editor edit = sharep.edit();
                                edit.putString("status", status);
                                edit.putString("version", version);
                                edit.commit();
                            } else {
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    } catch (UnsupportedEncodingException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
            }
        });
    }
}
